package com.zxy.supplier_system.server.utils

import jakarta.servlet.http.HttpServletRequest
import org.springframework.http.HttpStatus
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes
import org.springframework.web.server.ResponseStatusException

fun getCurrentServletRequest(): HttpServletRequest? {
    return (RequestContextHolder.getRequestAttributes() as? ServletRequestAttributes)?.request
}

fun <T> T?.orElse404(message: String?= null ): T {
    return this ?: throw ResponseStatusException(HttpStatus.NOT_FOUND,message)
}

fun <T> T?.orElse500ResourceNotFound(): T {
    return this ?: throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR,"资源未找到")
}